package com.example.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.example.im.IMInsert;
import com.example.im.IMReceiveService;
import com.example.jpush.bean.JPushMessageObj;
import com.example.util.finallist.SkKey;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;


import cc.vv.lklibrary.log.LogOperate;
import cn.jpush.android.api.JPushInterface;

/**
 * 推送消息接收器
 *
 * @author Zml
 * @data 2018/3/14.
 */
public class JPushReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG-Example";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundles = intent.getExtras();
        LogOperate.d( "JPush============[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundles));
        try {
            if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                String extraContent = bundles.getString(JPushInterface.EXTRA_EXTRA);
                LogOperate.d( "JPush============onReceive - " + extraContent);
                JPushMessageObj jPushMsgObj = new Gson().fromJson(extraContent, JPushMessageObj.class);
                LogOperate.d( "JPush============jPushMsgObj - " + jPushMsgObj);
                savePushMsg(jPushMsgObj);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                if (bundles != null) {
                    String extraContent = bundles.getString(JPushInterface.EXTRA_EXTRA);
                    JPushMessageObj jPushMsgObj = new Gson().fromJson(extraContent, JPushMessageObj.class);
                    // 打开主界面
                }

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                String extraContent = bundles.getString(JPushInterface.EXTRA_EXTRA);
                LogOperate.d( "JPush============自定义消息 - " + intent.getAction() + ", extras: " + extraContent);


               // LogOperate.d( "JPush============[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundles));


                // 是应用消息才保存
//                if (null != jPushMsgObj.type) {
//                    if (BTKey.BTKEY_String_1.equals(jPushMsgObj.type)) {
//                        PushContentObj pushContent = new Gson().fromJson(jPushMsgObj.pushContent, PushContentObj.class);
//                        /*
//                          保存推送消息
//                         */
//                        savePushMsg(jPushMsgObj, pushContent);
//                        /*
//                          收到推送消息时更新联系人表
//                         */
//                        createUpdateAccountTable(jPushMsgObj, pushContent);
//                    }
//                }

            }
        } catch (Exception e) {

        }

    }

    /**
     * 保存推送消息
     *
     * @param messageObj 极光推送的消息体
     */
    private void savePushMsg(JPushMessageObj messageObj) {
        String id = messageObj.type;
        String content = "";
        if (TextUtils.isEmpty(messageObj.title)){
            content=messageObj.content;
        }else{
            content=messageObj.title;
        }
         if (!TextUtils.isEmpty(content)){
             IMInsert.Companion.getInstance().receiveSingleTxt(SkKey.Companion.getSK_SYSTEM_MSG(), content, 0,id);
             IMReceiveService.Companion.getInstance().sendRefreshConversation();
         }

    }


    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                LogOperate.e("Message=====mmmm"+bundle.getString(JPushInterface.EXTRA_EXTRA));
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    LogOperate.e("Message=====sssss"+json.toString());
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                    LogOperate.e("Message====="+json.optString("MESSAGE"));
                } catch (JSONException e) {
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

}
